package com.lt.contract.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.lt.contract.bean.BASE_DATA;
import com.lt.contract.bean.utils.DownloadFileUtil;
import com.lt.contract.bean.utils.SelectListVO;
import com.lt.contract.bean.utils.SelectVO;
import com.lt.contract.service.CommonCService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping(value = "/common")
public class CommonCController {

    @Autowired
    CommonCService commonCService;

    /**
     * 获取  根据业务id 搜索字典表
     * @param params
     * @return
     */
    @CrossOrigin
    @ResponseBody
    @GetMapping("/getBaseData")
    public JSONArray getBaseData(@RequestParam String params){
        try {
            Map<String,Object> map = JSON.parseObject(params);
            List<BASE_DATA> list = commonCService.getBaseDataList(map);
            JSONArray result = JSON.parseArray(JSON.toJSONString(list));
            return result;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }

    }

    /**
     * 获取  根据业务类型 搜索字典表
     * @return
     */
    @CrossOrigin
    @ResponseBody
    @GetMapping("/getBaseDataList")
    public JSONArray getBaseDataList(){
        try {
            Map<String,Object> map = new HashMap<>();
            List<SelectListVO> list = commonCService.getParentType(map);
            JSONArray result = JSON.parseArray(JSON.toJSONString(list));

            return result;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }

    }


    /**
     * 获取  获取部门
     * @return
     */
    @CrossOrigin
    @ResponseBody
    @GetMapping("/getDept")
    public JSONArray getDept(){
        try {
//            Map<String,Object> map = new HashMap<>();
            List<SelectVO> list = commonCService.queryAllDept();
            JSONArray result = JSON.parseArray(JSON.toJSONString(list));
            return result;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }

    }

    /**
     * 获取  部门id 获取 部门成员
     * @return
     */
    @CrossOrigin
    @ResponseBody
    @GetMapping("/getDeptStaff")
    public JSONArray getDeptStaff(@RequestParam String param){
        try {
            Map<String,Object> map = JSON.parseObject(param);
            List<SelectVO> list = commonCService.queryAllDeptStaff(map);
            JSONArray result = JSON.parseArray(JSON.toJSONString(list));
            return result;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }

    }

    //文件上级目录
    String PATH = "";
    //文件名
    String FILENAME ="user.xlsx";
    //下载展示的文件名
    String NEWNAME="";
    /**
     * 下载模板
     * @return 返回excel模板
     */
    @RequestMapping(value = "/downloadWhpModel", method = RequestMethod.GET, produces ="application/json;charset=UTF-8")
    @ResponseBody
    public Object downloadModel(@RequestParam String fileName){
        ResponseEntity<InputStreamResource> response = null;
        String newName = fileName.substring(0,fileName.lastIndexOf("."));
        try {
            response = DownloadFileUtil.download(PATH, fileName, newName);
        } catch (Exception e) {
            log.error("下载模板失败");
        }
        return response;
    }

    @RequestMapping("/query")
    public void asyncTask() throws Exception {
        System.out.println("我是控制类里面的方法，我正在思考...............");
    }



}
